package com.stp.oversold.controller;

import com.stp.oversold.constant.R;
import com.stp.oversold.constant.queue.SecondKillQueue;
import com.stp.oversold.entity.SuccessKilled;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Random;
@Slf4j
@RestController
@RequiredArgsConstructor
public class OversoldQueueController {
    @PostMapping("/start/queue")
    public R startQueue(long skgId){
        try {
            log.info("开始秒杀方式六...");
            final long userId = (int) (new Random().nextDouble() * (99999 - 10000 + 1)) + 10000;
            SuccessKilled kill = new SuccessKilled();
            kill.setSecondKillId(skgId);
            kill.setUserId(userId);
            Boolean flag = SecondKillQueue.getSkillQueue().produce(kill);
            // 虽然进入了队列，但是不一定能秒杀成功 进队出队有时间间隙
            if(flag){
                log.info("用户:{}{}",kill.getUserId(),"秒杀成功");
            }else{
                log.info("用户:{}{}",userId,"秒杀失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return R.ok();
    }
}
